package com.hzj.recipe.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.api.R;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hzj.recipe.entity.Admin;
import com.hzj.recipe.entity.User;
import com.hzj.recipe.mapper.AdminMapper;
import com.hzj.recipe.service.AdminService;
import com.hzj.recipe.service.LoginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import java.util.Objects;

/**
* @author 蜗牛菌
* @description 针对表【admin(管理员表)】的数据库操作Service实现
* @createDate 2024-01-04 10:49:47
*/
@Service
public class AdminServiceImpl extends ServiceImpl<AdminMapper, Admin>
    implements AdminService{

    @Autowired
    private AdminMapper adminMapper;

    @Autowired
    @Lazy
    private LoginService loginService;

    @Override
    public Object listAll(Integer index, Integer size, Admin admin) {
        //默认第一页开始，每页5条
        if(index == null ||index==0){
            index=1;
        }
        if(size==null||size==0){
            size=5;
        }
        Page<Admin> pageInfo = new Page(index, size);
        LambdaQueryWrapper<Admin> lambdaQueryWrapper = new LambdaQueryWrapper();
        if (admin.getAdminId() != null && admin.getAdminId() != 0 && !Objects.equals(admin.getAdminId(), "")) {
            lambdaQueryWrapper.eq(Admin::getAdminId, admin.getAdminId());
        }
        if ((admin.getAdminName()!=null) && !Objects.equals(admin.getAdminName(), "")) {
            lambdaQueryWrapper.like(Admin::getAdminName, admin.getAdminName());
        }
        if ((admin.getAdminPhone()!=null) && !Objects.equals(admin.getAdminPhone(), "")) {
            lambdaQueryWrapper.like(Admin::getAdminPhone, admin.getAdminPhone());
        }
        if ((admin.getAdminEmail()!=null) && !Objects.equals(admin.getAdminEmail(), "")) {
            lambdaQueryWrapper.like(Admin::getAdminEmail, admin.getAdminEmail());
        }
        Page<Admin> adminPage = adminMapper.selectPage(pageInfo, lambdaQueryWrapper);

        return adminPage;

    }

    @Override
    public Admin findAdmin(String adminLoginId, String adminLoginPassword) {
        LambdaQueryWrapper<Admin> queryWrapper = new LambdaQueryWrapper<>();
        if (adminLoginId.contains("@")) {
            queryWrapper.eq(Admin::getAdminEmail, adminLoginId);
        } else {
            queryWrapper.eq(Admin::getAdminPhone, adminLoginId);
        }
        queryWrapper.eq(Admin::getAdminPassword, adminLoginPassword);
        //查到一条符合就不再往下查找了
        queryWrapper.last("limit 1");

        return this.adminMapper.selectOne(queryWrapper);
    }

    @Override
    public R selectCurrentUser(String token) {
        Admin admin = this.loginService.checkAdminToken(token);
        //验证token是否合法
        if (admin == null) {
            //验证失败，返回错误信息
            return R.failed("token不合法");
        }
        //验证成功，返回对应结果
        return R.ok(admin);
    }

    //根据电话或邮箱查找账号是否存在
    @Override
    public Admin findPE(String adminPhoneOrEmail) {
        LambdaQueryWrapper<Admin> queryWrapper = new LambdaQueryWrapper<>();
        if (adminPhoneOrEmail.contains("@")) {
            queryWrapper.eq(Admin::getAdminEmail, adminPhoneOrEmail);
        } else {
            queryWrapper.eq(Admin::getAdminPhone, adminPhoneOrEmail);
        }
        //查到一条符合就不再往下查找了
        queryWrapper.last("limit 1");

        return this.adminMapper.selectOne(queryWrapper);
    }
}




